package com.team.ai.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.team.ai.entity.ResultEntity;
import com.team.ai.entity.User;
import com.team.ai.mapper.UserMapper;
import com.team.ai.service.CommunityService;
import com.team.ai.tool.KoDoTool;
import jakarta.annotation.Resource;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

@CrossOrigin//跨域
@RestController//遵循RestFul
@RequestMapping("v2/community/")
public class CommunityController {
    @Resource
    private RedisTemplate<String, String> redisTemplate;
    @Resource
    CommunityService communityService;

    @Resource
    UserMapper userMapper;
    @Resource
    KoDoTool koDoTool;


    @Value("${project.localfilepath}")
    private String localFilePath;//本地文件存储地址
    @SaCheckLogin
    @PostMapping("insert")
    public String insert(
            @RequestParam(value = "content",defaultValue = "-1") String content,
            @RequestParam("file")   Optional<List<MultipartFile>> file
//            @RequestParam("file")   List<MultipartFile> file


    )  {
        int userId = StpUtil.getLoginIdAsInt();
        User user = userMapper.selectByPrimaryKey(userId);
        String uName=user.getName();
        String img=user.getAvatar();
        if(file.isEmpty()){
            return communityService.insert(userId,content,null,uName,img);
        }else {
            List<String> urlList = new ArrayList<>();
            List<MultipartFile> files = file.get();
            for (int j=0;j<files.size();j++){
                String SuffixName = files.get(j).getOriginalFilename().substring(files.get(j).getOriginalFilename().lastIndexOf("."));//获取文件后缀名
                String FileNewName = UUID.randomUUID() + SuffixName;//新的文件名
                File dest = new File(localFilePath + FileNewName);//生成新的文件
                if (!dest.getParentFile().exists()) {//判定文件夹是否存在
                    dest.getParentFile().mkdir();//生成文件夹
                }
                try {
                    files.get(j).transferTo(dest);//将临时文件转为正式文件
                    String koDoString = koDoTool.UploadFile(localFilePath + FileNewName);//上传到七牛云
                    urlList.add(koDoString);
                    //userService.addAvatar(id,koDoString);
                    //return communityService.insert(userId,content,urlList.toString());//返回七牛云的文件路径字符串
                    //return urlList.toString();
                } catch (Exception e) {
                    return ResultEntity.fail(-10001);//返回错误代码
                }
            }
            return communityService.insert(userId,content, urlList.toString(),uName,img);
        }

    }





//
//    @SaCheckLogin
//    @PostMapping("insert")
//    public String insert(
//            @RequestParam(value = "content",defaultValue = "-1") String content,
//            @RequestParam("pic")   String pic
//
//    )  {
//        int userId = StpUtil.getLoginIdAsInt();
//        User user = userMapper.selectByPrimaryKey(userId);
//        String uName=user.getName();
//        String img=user.getAvatar();
//        return communityService.insert(userId,content, pic,uName,img);
//    }




    @PostMapping("update")
    public String update(
            @RequestParam(value = "likesize",defaultValue = "0") int likesize,
            @RequestParam(value = "id",defaultValue = "0") int id
    ){

        return communityService.update(likesize,id);
    }





    @SaCheckLogin
    @GetMapping("selectList")
    public String selectList(){
        int userId = StpUtil.getLoginIdAsInt();
        return communityService.selectByUserIda(userId);
    }

    @GetMapping("selectById")
    public String selectById(
            @RequestParam(value = "id",defaultValue = "-1") int id,
            @RequestParam(value = "isgz",defaultValue = "0") int isgz,
            @RequestParam(value = "islike",defaultValue = "0") int islike

            ){
        return communityService.selectById(id,isgz,islike);
    }
    @SaCheckLogin
    @GetMapping("selectByUserId")
    public String selectByUserId(){
        int userId = StpUtil.getLoginIdAsInt();
        return communityService.selectByUserId(userId);
    }

}
